// pages/my/my-faultRepair/my-faultRepair.js
const request = require('../../../utils/request.js');
//获取应用实例
const app = getApp()
var pic;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curId:1,
    curTitle:"柜门无法打开",
    imageList: [],
    uploadPicBtn: true,
  },
  radioChange: function (e) {
    var that = this;
    console.log(e)
    that.setData({
      curId: e.currentTarget.dataset.id,
      curTitle: e.currentTarget.dataset.title
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      that.getToken();
  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })

      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
  // 选择照片
  chooseImage: function () {
    var that = this;
    var tempCount = 3 - that.data.imageList.length;
    if (tempCount < 1) {
      that.setData({
        uploadPicBtn: false
      })
    }
    wx.chooseImage({
      count: tempCount,
      sizeType: 'compressed',
      success: function (res) {
        console.log(res);
        console.log(tempCount);
        var tempImageList = that.data.imageList.concat(res.tempFilePaths);
        pic = 1;
        that.setData({
          imageList: tempImageList
        })
        if (tempImageList.length == 3) {
          that.setData({
            uploadPicBtn: false
          })
        }
      }
    })
  },
  //预览照片
  previewImage: function (e) {
    console.log(this.data.imageList);
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  // 删除未上传图片
  delPic: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.imageList.splice(index, 1);
    that.setData({
      imageList: that.data.imageList,
      uploadPicBtn: true
    });
  },



  //表单提交
  formSubmit: function (e) {
    var that = this;
    console.log(e.detail.value)

    if (e.detail.value.desc == "" || e.detail.value.desc == undefined) {
      wx.showToast({
        title: '请输入描述',
        image: "../../../images/xx.png"
      })
    } else {
      that.setData({
        chooseType: e.detail.value.chooseType,
        desc: e.detail.value.desc,
      })
      console.log(that.data)
      that.upload();
    }
  },

  upload: function () {
    var that = this;
    //新增报修申请
    console.log({
      "session_id": that.data.token,
      'type': that.data.curTitle,
      'content': that.data.desc
    })
    request.addBaoxiu(
      {
        "session_id": that.data.token,
        'type': that.data.curTitle,
        'content': that.data.desc
      },
      (res) => {
        console.log(res);
        console.log(pic);

        if (res.data.code == "0") {
          if (pic) {//有图片
            console.log(pic)
            var i = 0;
            for (i; i < this.data.imageList.length; i++) {
              //退货申请添加图片
              request.addPic(
                {
                  "id": res.data.lastid,
                }, that.data.imageList[i],
                (res) => {
                  console.log(res);
                },
              )
            }
            if (i = this.data.imageList.length) {
              wx.showModal({
                title: '温馨提示',
                content: "提交成功",
                showCancel: false,
                confirmColor: "#FF0000",
                success: function (res) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            }
          } else {//没有图片

            wx.showModal({
              title: '温馨提示',
              content: "提交成功",
              showCancel: false,
              confirmColor: "#FF0000",
              success: function (res) {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false,
            confirmColor: "#FF0000",
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      },
    )
  }
})